package com.leecreate.common.utils;

import com.leecreate.common.utils.spring.SpringUtils;
import io.minio.*;
import io.minio.http.Method;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * Minio 文件存储工具类
 *
 * @author ruoyi
 */
public class MinioUtil {
    /**
     * 上传文件
     *
     * @param bucketName 桶名称
     * @param fileName
     * @throws IOException
     */
    public static String uploadFile(String bucketName, String fileName, MultipartFile multipartFile) throws IOException {
        String url = "";
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        try (InputStream inputStream = multipartFile.getInputStream()) {
            minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileName).stream(inputStream, multipartFile.getSize(), -1).contentType(multipartFile.getContentType()).build());
            url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(fileName).method(Method.GET).build());
            url = url.substring(0, url.indexOf('?'));
            return ServletUtils.urlDecode(url);
        } catch (Exception e) {
            throw new IOException(e.getMessage(), e);
        }
    }

    public static void downloadFile(String bucketName, String objectName, String destinationPath) throws Exception {
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        try (InputStream stream = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build())) {
            Files.copy(stream, Paths.get(destinationPath));
            System.out.println("File downloaded successfully.");
        }
    }

    public static String getFileUrl(String bucketName, String objectName) throws Exception {
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(objectName).method(Method.GET).build());
    }

    public static void deleteFile(String bucketName, String objectName) throws Exception {
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
        System.out.println("File deleted successfully.");
    }

    public static void uploadFile(String bucketName, String objectName, String filePath) throws Exception {
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        try (InputStream inputStream = new FileInputStream(filePath)) {
            minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(inputStream, inputStream.available(), -1).contentType("application/octet-stream").build());
            System.out.println("File uploaded successfully.");
        }
    }

    public static void createBucket(String bucketName) throws Exception {
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!found) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            System.out.println("Bucket created successfully.");
        } else {
            System.out.println("Bucket already exists.");
        }
    }
}
